package com.mingtu.guards.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.utils.CalendarPickerUtils;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.adapter.TabbarAdapter;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.ApproveFragment;
import com.mingtu.guards.utils.MyConstant;
import com.mingtu.guards.utils.MySPTools;
import com.mingtu.guards.view.FilterCancleView;
import com.mingtu.guards.view.FilterSureView;
import com.mingtu.guards.view.TimeFilterView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class ApproveActivity extends MyBaseActivity {

    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.iv_filte)
    ImageView ivFilte;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.layout_right_drawer)
    LinearLayout layoutRightDrawer;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.img_plus)
    ImageView imgPlus;
    @BindView(R.id.time_filter_time)
    TimeFilterView timeFilterTime;
    @BindView(R.id.filter_cancle_view)
    FilterCancleView filterCancleView;
    @BindView(R.id.filter_sure_view)
    FilterSureView filterSureView;
    private String keyword;
    private int menuPosition;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String beginTime;
    private String endTime;
    private ApproveFragment approveFragment1;
    private ApproveFragment approveFragment2;
    private ApproveFragment approveFragment3;
    private int type = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_approve;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("审批");
    }

    @Override
    protected void initData() {
        boolean isLeader = SPStaticUtils.getBoolean(SPTools.isLeader);
        if (isLeader) {
            type = 0;
        } else {
            type = 1;
        }

        if (type == 0) {
            setModuleTitle("审批");
        } else if (type == 1) {
            setModuleTitle("我的申请");
        }

        SPStaticUtils.put(MySPTools.approveFliteBeginTime, "");
        SPStaticUtils.put(MySPTools.approveFliteEndTime, "");
        SPStaticUtils.put(MySPTools.approveSearchKey, "");

        approveFragment1 = ApproveFragment.getInstance(0, type);
        approveFragment2 = ApproveFragment.getInstance(1, type);
        approveFragment3 = ApproveFragment.getInstance(2, type);

        mFragments.add(approveFragment1);
        mFragments.add(approveFragment2);
        mFragments.add(approveFragment3);

        viewPager.setOffscreenPageLimit(3);//需要设置此值
        viewPager.setAdapter(new TabbarAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new myOnPageChangeListener());

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH || i == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    //此处做逻辑处理
                    keyword = UIUtils.getTextView(textView);

                    SPStaticUtils.put(MySPTools.approveSearchKey, keyword);
                    approveFragment1.refreshData();
                    approveFragment2.refreshData();
                    approveFragment3.refreshData();

                    return true;
                }
                return false;
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        menuPosition = 0;
                        viewPager.setCurrentItem(0, true);
                        break;
                    case R.id.rb2:
                        menuPosition = 1;
                        viewPager.setCurrentItem(1, true);
                        break;
                    case R.id.rb3:
                        menuPosition = 1;
                        viewPager.setCurrentItem(2, true);
                        break;
                }
            }
        });

        EventBus.getDefault().register(this);

        timeFilterTime.setOnTimeResultListener(new TimeFilterView.OnTimeResultListener() {
            @Override
            public void onResult(Date startDate, Date endDate) {
                beginTime = UIUtils.getTime3(startDate);
                endTime = UIUtils.getTime3(endDate);

                SPStaticUtils.put(MySPTools.approveFliteBeginTime, beginTime);
                SPStaticUtils.put(MySPTools.approveFliteEndTime, beginTime);
            }
        });

        filterCancleView.setOnClickListener(new FilterCancleView.OnClickListener() {
            @Override
            public void onClickCancel() {
                closeDrawers();
            }
        });

        filterSureView.setOnClickListener(new FilterSureView.OnClickListener() {
            @Override
            public void onClickReset() {
                closeDrawers();
                resetData();
                CalendarPickerUtils.getInstance().resetData();
            }

            @Override
            public void onClickSure() {
                boolean isResumed1 = approveFragment1.isResumed(); //判断fragment是否已经加载
                boolean isResumed2 = approveFragment2.isResumed();
                boolean isResumed3 = approveFragment3.isResumed();

                if (isResumed1) {
                    approveFragment1.refreshData();
                }
                if (isResumed2) {
                    approveFragment2.refreshData();
                }
                if (isResumed3) {
                    approveFragment3.refreshData();
                }

                closeDrawers();
            }
        });

    }

    @Override
    protected void getData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        SPStaticUtils.remove(MySPTools.approveFliteBeginTime);
        SPStaticUtils.remove(MySPTools.approveFliteEndTime);
        SPStaticUtils.remove(MySPTools.approveSearchKey);
    }

    @OnClick({R.id.tv_search, R.id.iv_filte, R.id.tv_end_time,  R.id.layout_right_drawer, R.id.img_plus})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_right_drawer:
                //不做任何操作，防止点击穿透
                break;
            case R.id.tv_search:
                keyword = UIUtils.getEditText(editSearch);
                SPStaticUtils.put(MySPTools.approveSearchKey, keyword);

                approveFragment1.refreshData();
                approveFragment2.refreshData();
                approveFragment3.refreshData();

                break;
            case R.id.iv_filte:
                boolean isOpen = drawerLayout.isDrawerOpen(layoutRightDrawer);
                if (isOpen) {
                    closeDrawers();
                } else {
                    openDrawer();
                }
                break;
            case R.id.img_plus:
                IntentUtils.getInstance().readyGo(ActivityUtils.getTopActivity(), ApproveApplyActivity.class);
                break;
        }
    }

    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //处理完之后刷新数据
        String message = event.getMessgae();
        if (message.equals(MyConstant.REFRESH_APPROVE_DATA)) {
            keyword = "";
            editSearch.setText("");
            SPStaticUtils.put(MySPTools.approveSearchKey, "");
            approveFragment1.refreshData();
            approveFragment2.refreshData();
            approveFragment3.refreshData();
        }

    }

    /**
     * 关闭侧边栏
     */
    public void closeDrawers() {
        drawerLayout.closeDrawer(layoutRightDrawer, true);
    }

    /**
     * 打开侧边栏
     */
    public void openDrawer() {
        drawerLayout.openDrawer(layoutRightDrawer);
    }

    /**
     * 重置数据
     */
    private void resetData() {
        SPStaticUtils.put(MySPTools.approveFliteBeginTime, "");
        SPStaticUtils.put(MySPTools.approveFliteEndTime, "");
        SPStaticUtils.put(MySPTools.approveSearchKey, "");

        editSearch.setText("");
        timeFilterTime.resetData();

        approveFragment1.refreshData();
        approveFragment2.refreshData();
        approveFragment3.refreshData();

    }


    private class myOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    radioGroup.check(R.id.rb1);
                    break;
                case 1:
                    radioGroup.check(R.id.rb2);
                    break;
                case 2:
                    radioGroup.check(R.id.rb3);
                    break;
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}